import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
//element ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
//路由
import VueRouter from 'vue-router'
//状态管理工具
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
//import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import axios from "axios";
//axios.defaults.baseURL="/api"
axios.defaults.baseURL="http://localhost:8081"
//给Vue设置一个属性叫做$http
Vue.prototype.$http=axios

import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: 'Vhkl4eL8qLZRBhE9CmCVYolu22smNUqu'
})

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
/*
  拦截器:
    axios 的请求拦截器
      在里面做配置,在请求头里面携带token
      axios在向后端发起请求前,都会经过该拦截器,判断localStorage中是否有token,如果有就放到请求头并传到后端去

    axios 的响应拦截器
      在里面做配置,如果登录过期,跳转到登录页面
 */
axios.interceptors.request.use(config=>{
  //1.获取localStroage里面的token
  let token = localStorage.getItem("token");
  //2.判断token 如果不为null 把token加在请求头里面
  if (token){
    config.headers.token=token;
  }
  return config;
},error => {
  return Promise.reject(error);
})


axios.interceptors.response.use(resp=>{
  let {success,msg}=resp.data
  // 如果后端返回信息为noLogin,说明未登录被后端登录拦截了,就要跳转到登录页面去,让用户做登录
  if (!success && msg === "noLogin"){
    alert("请登录后再来操作!")
    router.push({ path: '/login' });
  }
  return resp;
},error => {
  return Promise.reject(error);
})


/**
 * 前端做登录拦截,如果前端就可以拦截掉大部分未登录用户,可以降低后端压力
 */
/*
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    localStorage.removeItem('loginUser');
    localStorage.removeItem('token');
  }

  let user = JSON.parse(localStorage.getItem('loginUser'));
  // 如果当前用户要访问的不是登录页面,但是loginUser又等于空,说明当前用户在访问受限资源,且没有登陆
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
*/



//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

